{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96f97dbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from onekey_algo import get_param_in_cwd\n",
    "from onekey_algo.custom.components.metrics import get_time_dependent_gt\n",
    "from onekey_algo.custom.components.comp1 import normalize_df\n",
    "\n",
    "\n",
    "event_col = get_param_in_cwd('event_col')\n",
    "duration_col = get_param_in_cwd('duration_col')\n",
    "model_names = [mn for mn in get_param_in_cwd('summary_models')]\n",
    "joinit_info = pd.read_csv('results/joinit_info.csv', dtype={'ID':str})\n",
    "\n",
    "joinit_info = normalize_df(joinit_info, not_norm=['ID', event_col, duration_col,'group'], method='minmax')\n",
    "joinit_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "835a9a9e",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from onekey_algo.custom.components.comp1 import merge_results, draw_roc\n",
    "from onekey_algo.custom.components.metrics import analysis_pred_binary\n",
    "from onekey_algo.custom.components.comp1 import draw_calibration\n",
    "from onekey_algo.custom.components.comp1 import plot_DCA\n",
    "metric = []\n",
    "youden = {}\n",
    "mall = []\n",
    "\n",
    "for tname, time in get_param_in_cwd('time_settings').items():\n",
    "    gts = []\n",
    "    preds = []\n",
    "    for subset in get_param_in_cwd('subsets'):\n",
    "        data = joinit_info[joinit_info['group'] == subset]\n",
    "        time_label_data = get_time_dependent_gt(data, time=time, event_col=event_col, duration_col=duration_col)\n",
    "        if len(np.unique(time_label_data['label'])) == 1:\n",
    "            continue\n",
    "        time_label_data['ID'] = time_label_data['ID'].astype(str)\n",
    "        ALL_results = merge_results(data, time_label_data)\n",
    "        ALL_results = ALL_results.drop_duplicates('ID')\n",
    "        print(tname, subset, ALL_results.shape)\n",
    "        ALL_results.to_csv(f'results/time_roc_{tname}_{subset}.csv', index=False)\n",
    "        gt = [1-np.array(ALL_results['label']) for _ in model_names]\n",
    "        pred_train = [np.array(ALL_results[d]) for d in model_names]\n",
    "        gts.extend(gt)\n",
    "        preds.extend(pred_train)\n",
    "        draw_roc(gt, pred_train, labels=model_names, title=f'Cohort {subset} {tname} ROC')\n",
    "        plt.savefig(f'img/{tname}_{subset}_auc.svg')\n",
    "        plt.show()\n",
    "        \n",
    "        plot_DCA(pred_train, gt[0], title=f'Cohort {subset} {tname} DCA', labels=model_names, y_min=-0.15, remap=True)\n",
    "        plt.savefig(f'img/{tname}_{subset}_dca.svg')\n",
    "        plt.show()\n",
    "        \n",
    "        draw_calibration(pred_scores=pred_train, n_bins=5, remap=True, y_test=gt, model_names=model_names)\n",
    "        plt.title(f\"Cohort {subset} {tname} Calibration\")\n",
    "        plt.savefig(f'img/{tname}_{subset}_cali.svg')\n",
    "        plt.show()\n",
    "        for mname, y, score in zip(model_names, gt, pred_train):\n",
    "            # 计算验证集指标\n",
    "            acc, auc, ci, tpr, tnr, ppv, npv, precision, recall, f1, thres = analysis_pred_binary(y, score)\n",
    "            ci = f\"{ci[0]:.4f} - {ci[1]:.4f}\"\n",
    "            metric.append((mname, acc, auc, ci, tpr, tnr, ppv, npv, tname, f\"{subset[0].upper() + subset[1:]}\"))\n",
    "            youden[mname] = thres\n",
    "            \n",
    "\n",
    "time_metrics = pd.DataFrame(metric, index=None, columns=['Signature', 'Accuracy', 'AUC', '95% CI', 'Sensitivity', 'Specificity', \n",
    "                                                         'PPV', 'NPV', 'Survival', 'Cohort'])\n",
    "time_metrics.to_csv('results/time_dependent_metrics.csv', index=False)\n",
    "time_metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6184167",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
